import logging
import time
import os


def get_logger(logdir="logs", logname=None):
    if not os.path.exists(logdir):
        os.mkdir(logdir)
        
    if not logname:
        filename = "{}.log".format(time.strftime("%Y-%m-%d",time.localtime()))
    else:
        filename = logname + ".log"
    filename = os.path.join(logdir,filename)
    filename = os.path.normpath(filename)
    
    logger = logging.getLogger("mylogger")
    logger.setLevel(logging.DEBUG)
    if not logger.handlers: #作用,防止重新生成处理器
        sh = logging.StreamHandler()#创建控制台日志处理器
        fh = logging.FileHandler(filename=filename,mode='a',encoding="utf-8")#创建日志文件处理器
    formatter = logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s")
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)
    logger.addHandler(sh)
    logger.addHandler(fh)
    return logger
    